package src.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ListSelectionListener;

import src.model.DbContext;
import src.model.entity.Movies;
import src.model.entity.Saloons;
import src.model.entity.Seans;
import src.model.entity.Tickets;
import src.model.repository.MoviesRES;

public class runProject {

	private JFrame mainframe;
	private JList<String> list;
	private DefaultListModel<String> listModel;
	private JLabel movieName;
	private JLabel directorName;
	private JLabel actorsNames;
	private JLabel saloonNumber;
	private JLabel movieTypes;
	private JLabel movieYear;
	private JButton biletAlBtn;
	private JComboBox<String> seansCMBX;
	private DefaultComboBoxModel<String> defaultCBM;
	private JButton oKbtn;
	private JPanel centerpanel;
	private JFrame seatsFrame;

	static {
		LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
		try {
			UIManager.setLookAndFeel(lookAndFeels[1].getClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public runProject() {
		loadCompanents();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void loadCompanents() {
		mainframe = new JFrame();
		mainframe.setTitle("Cinema Seans Program");
		mainframe.setSize(600, 600);
		mainframe.setLocationRelativeTo(null);
		mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panelCenter = new JPanel();
		BoxLayout layout = new BoxLayout(panelCenter, BoxLayout.X_AXIS);
		panelCenter.setLayout(layout);
		JPanel leftPanel = new JPanel();
		leftPanel.setBorder(BorderFactory.createTitledBorder("Filmler"));
		listModel = new DefaultListModel();
		listModel.clear();
		list = new JList(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setSize(80, 80);
		list.setSelectedIndex(0);
		leftPanel.add(list);
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new GridLayout(7, 1));
		rightPanel.setBorder(BorderFactory.createTitledBorder("Film Detaylar"));
		movieName = new JLabel();
		directorName = new JLabel();
		actorsNames = new JLabel();
		saloonNumber = new JLabel();
		movieTypes = new JLabel();
		movieYear = new JLabel();
		rightPanel.add(movieName);
		rightPanel.add(directorName);
		rightPanel.add(actorsNames);
		rightPanel.add(movieTypes);
		rightPanel.add(movieYear);
		rightPanel.add(saloonNumber);
		biletAlBtn = new JButton("Bilet Al");
		rightPanel.add(biletAlBtn);
		panelCenter.add(leftPanel);
		panelCenter.add(rightPanel);
		mainframe.add(panelCenter, BorderLayout.CENTER);

	}

	public void makeVisible(boolean visible) {
		mainframe.setVisible(visible);
	}

	public void updateLabels(MoviesRES movies) {
		movieName.setText(movies.findById(movieindex()).getMovieName());
		directorName.setText(movies.findById(movieindex()).getDirectors()
				.getDirectorName());
		actorsNames.setText(movies.findById(movieindex()).getActors()
				.getActorName());
		movieTypes.setText(movies.findById(movieindex()).getTypes()
				.getTypesName());
		movieYear.setText(movies.findById(movieindex()).getYear());
		saloonNumber.setText(movies.findById(movieindex()).getSaloons()
				.getSaloonName());
	}

	public void updateListModel(List<Movies> movieNames) {
		listModel.clear();
		for (int i = 0; i < movieNames.size(); i++) {
			listModel.add(i, movieNames.get(i).getMovieName());
		}
		if (!listModel.isEmpty())
			listModel.firstElement();
	}
	public void updateComboList(List<Seans> seansTimes) {
		defaultCBM.removeAllElements();
		for (int i = 0; i < seansTimes.size(); i++) {
			defaultCBM.addElement(seansTimes.get(i).getSeansTime());
		}
	}

	public void biletAlAction(ActionListener biletAl) {
		biletAlBtn.addActionListener(biletAl);
	}
	public void listClick(ListSelectionListener listener) {
		this.list.addListSelectionListener(listener);
	}

	public int movieindex() {
		int x = list.getSelectedIndex()+1;
		return x;
	}
	
	public int Comboindex() {
		int x = seansCMBX.getSelectedIndex()+1;
		return x;
	}

	public void ChooseSeats(String MovieName) {
		seatsFrame = new JFrame();
		seatsFrame.setTitle(MovieName);
		seatsFrame.setSize(600, 600);
		seatsFrame.setLocationRelativeTo(null);
		seatsFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		JPanel panelCenter = new JPanel();
		// BoxLayout layout = new BoxLayout(panelCenter,BoxLayout.Y_AXIS);
		BorderLayout layout = new BorderLayout();
		panelCenter.setLayout(layout);
		JPanel upPanel = new JPanel();
		upPanel.setBorder(BorderFactory.createTitledBorder("Seans Secimi"));
		defaultCBM = new DefaultComboBoxModel<String>();
		// defaultCBM.addElement();
		seansCMBX = new JComboBox<String>(defaultCBM);
		upPanel.add(seansCMBX);
		panelCenter.add(upPanel, BorderLayout.NORTH);
		centerpanel = new JPanel();
		centerpanel
				.setBorder(BorderFactory.createTitledBorder("Koltuk Secimi"));
		centerpanel.setLayout(new GridLayout(5, 10, 10, 10));
		panelCenter.add(centerpanel, BorderLayout.CENTER);
		JPanel downpanel = new JPanel();
		oKbtn = new JButton("Onayla");
		onayla(oKbtn);
		downpanel.add(oKbtn);
		panelCenter.add(downpanel, BorderLayout.SOUTH);
		seatsFrame.add(panelCenter, BorderLayout.CENTER);
		seatsFrame.setVisible(true);
		seatsFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				makeVisible(true);
			}
		});
	}

	public void selectComboBox(Saloons SalonSeat) {
		
		seansCMBX.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(e);
				centerpanel.removeAll();
				if (Comboindex()==SalonSeat.getId()) {
					int koltukAdet = SalonSeat.getKoltukSayisi();
					for (int i = 0; i < koltukAdet; i++) {
						JButton koltuk = new JButton();
						koltuk.setText((i + 1) + ".koltuk");
						koltuk.setBackground(Color.RED);
						koltuk.setForeground(Color.WHITE);
						koltuk.setActionCommand(Integer.toString(i));
						koltukAction(koltuk);
						centerpanel.add(koltuk);
					}
				}
				centerpanel.repaint();
				centerpanel.revalidate();
				
			}
		});
	}

	public void onayla(JButton button) {
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int toplambiletAdet = 0;
				Component[] components = centerpanel.getComponents();
				for (int i = 0; i < components.length; i++) {
					Component currentComponent = components[i];
					if (currentComponent instanceof JButton) {
						JButton button = (JButton) currentComponent;
						if (button.getBackground() == Color.MAGENTA) {
							toplambiletAdet++;
						}
					}
				}

				int result = JOptionPane.showConfirmDialog(
						null,
						"Satin Alinacak Bilet Adedi :"
								+ Integer.toString(toplambiletAdet),
						"Bilet Al", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (result == JOptionPane.YES_OPTION) {
					DbContext database = DbContext.getInstance();
					Tickets tickets = new Tickets();
					for (int i = 0; i < components.length; i++) {
						Component currentComponent = components[i];
						if (currentComponent instanceof JButton) {
							JButton button = (JButton) currentComponent;
							if (button.getBackground() == Color.MAGENTA) {
								tickets.setMovies(database.getMoviesRES().findById(movieindex()));
								tickets.setSeans(database.getSeansRES().findById(Comboindex()));
								tickets.setSeatCase(true);
								tickets.setSeatNumber(Integer.parseInt(button.getActionCommand()));
								database.getTicketsRES().insert(tickets);
							}
						}
					}
				}else {
					for (int i = 0; i < components.length; i++) {
						Component currentComponent = components[i];
						if (currentComponent instanceof JButton) {
							JButton button = (JButton) currentComponent;
							if (button.getBackground() == Color.MAGENTA) {
								button.setBackground(Color.RED);
								button.setForeground(Color.WHITE);
							}
						}
					}
				}
			}
		});
	}

	public String SaloonName() {
		return saloonNumber.getText();
	}

	public void koltukAction(JButton koltuk) {
		koltuk.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (koltuk.getBackground() == Color.RED) {
					koltuk.setForeground(Color.WHITE);
					koltuk.setBackground(Color.MAGENTA);
				} else if (koltuk.getBackground() == Color.MAGENTA) {
					koltuk.setBackground(Color.RED);
					koltuk.setForeground(Color.WHITE);
				}
			}
		});
	}
}
